package com.crmweb.serviceImpl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.crmweb.dao.ModulesDao;
import com.crmweb.dto.TreeModules;
import com.crmweb.entity.DTree;
import com.crmweb.entity.Modules;
import com.crmweb.entity.Rolemodules;
import com.crmweb.service.ModulesService;
@Service
public class ModulesServiceImpl implements ModulesService{
@Autowired
private ModulesDao md;
	@Override
	public List<DTree> selModulesByWeight() {
		// TODO Auto-generated method stub
		return md.selModulesByWeight();
	}

	@Override
	public int addModules(Modules modules) {
		// TODO Auto-generated method stub
		return md.addModules(modules);
	}

	@Override
	public int updateModules(Modules modules) {
		// TODO Auto-generated method stub
		return md.updateModules(modules);
	}

	@Override
	public int delModules(int id) {
		// TODO Auto-generated method stub
		return md.delModules(id);
	}

	@Override
	public List<Rolemodules> selMidIsCanDel(int moduleId) {
		// TODO Auto-generated method stub
		return md.selMidIsCanDel(moduleId);
	}

	@Override
	public Modules checkMNameCF(String name, int parentId) {
		// TODO Auto-generated method stub
		return md.checkMNameCF(name, parentId);
	}

	@Override
	public List<TreeModules> selTreeMByOrderWeight() {
		/*
		 * // TODO Auto-generated method stub List<Modules> list=selModulesByWeight();
		 * List<TreeModules> tlist=new ArrayList<TreeModules>(); for (Modules modules :
		 * list) {
		 * 
		 * TreeModules t=new TreeModules(); t.setId(modules.getId());
		 * t.setName(modules.getName()); t.setPath(modules.getPath());
		 * t.setParentId(String.valueOf(modules.getParentId()));
		 * t.setWeight(modules.getWeight()); tlist.add(t); } return tlist;
		 */
		return null;
	}

	@Override
	public List<TreeModules> queryTreeByTM(List<TreeModules> nodes) {
		// TODO Auto-generated method stub
		List<TreeModules> plist=new ArrayList<TreeModules>();
		for (TreeModules ptree : nodes) {
			if(ptree.getParentId().equals("0")) {
				plist.add(ptree);
			}
		}
		for (TreeModules tree : plist) {
			tree.setChildren(getChild(String.valueOf(tree.getId()), nodes));
		}
		return plist;
	
}
	public List<TreeModules> getChild(String pid,List<TreeModules> list){
		List<TreeModules> clist=new ArrayList<TreeModules>();
		for (TreeModules ctree : list) {
			if(ctree.getParentId().equals(pid)) {
				clist.add(ctree);
			}
		}
		for (TreeModules cctree : clist) {
			cctree.setChildren(getChild(String.valueOf(cctree.getId()), list));
		}
		return clist;
	}

	@Override
	public List<Integer> selModulesBymid(List<Integer> list) {
		// TODO Auto-generated method stub
		return md.selModulesBymid(list);
	}

}
